package br.com.rafsoft.mapaentrega.activity;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import br.com.rafsoft.mapaentrega.MapaEntregaApplication;
import br.com.rafsoft.mapaentrega.R;
import br.com.rafsoft.mapaentrega.adapter.MessageAdapter;
import br.com.rafsoft.mapaentrega.shared.MessageProxy;

public class MessageViewActivity extends Activity {

	private java.text.DateFormat dateFormat;
	private Intent intentNew;

	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.message_view);

		dateFormat = DateFormat.getDateFormat(MessageViewActivity.this);

		// get the task to display.
		Intent intent = getIntent();
		int position = intent.getIntExtra("position", -1);

		TextView from = (TextView) findViewById(R.id.textViewMessageViewFrom);
		TextView date = (TextView) findViewById(R.id.textViewMessageViewDate);
		TextView time = (TextView) findViewById(R.id.textViewMessageViewTime);
		TextView text = (TextView) findViewById(R.id.textViewMessageViewText);

		if (position != -1) {
			MapaEntregaApplication mapaEntregaApplication = (MapaEntregaApplication) getApplication();
			MessageAdapter adapter = mapaEntregaApplication
					.getMessageAdapter(this);

			MessageProxy message = adapter.getItem(position);

			if (message != null) {
				// Seta os valores da mensagem
				from.setText(message.getUserFrom());
				text.setText(message.getText());
				Date dateMessage = message.getDate();
				date.setText(dateFormat.format(dateMessage));
				SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");
				String timeMessage = sdf.format(dateMessage);
				time.setText(timeMessage);
			}
			
			intentNew = new Intent(MessageViewActivity.this, MessageNewActivity.class);
			intentNew.putExtra("userTo", from.getText());
			
			Button button = (Button) findViewById(R.id.buttonMessageReply);
			button.setOnClickListener(new Button.OnClickListener() {

				@Override
				public void onClick(View v) {
					startActivityForResult(intentNew, 0);
				}
			});
		}

	}

	@Override
	protected void onActivityResult(int code, int result, Intent intent) {
		if (result == RESULT_OK) {
			setResult(RESULT_OK);
			finish();
		}
	}

}
